home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / dev / lang / sbp3_1e.lzh / PLANNER.PL < prev    next >
Text File  |  1991-10-31  |  3KB  |  87 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5. /* Modified for Quintus Prolog by Andreas Siebert */
  6.  
  7. /* PLANNER.PL */
  8.  
  9. /* This program uses FINDALL.PL from Chapter 6. */
  10. :- ( clause(find_all(_,_,_),_) ; consult('findall.pl') ).
  11.  
  12. itinerary(Start,Destination) :-
  13.      flight_planner(Destination,[Start],_).
  14.  
  15. flight_planner(Destination,[Destination|RestOfCities],
  16.      [Destination|RestOfCities]).
  17.  
  18. flight_planner(Destination,[OldCity|RestOfCities],Solution) :-
  19.      find_all(X,connection(OldCity,X),List),
  20.      best(Destination,List,NewCity),
  21.      \+ member(NewCity,RestOfCities),
  22.      write([NewCity,OldCity|RestOfCities]), nl,
  23.      flight_planner(Destination,[NewCity,OldCity|RestOfCities],Solution).
  24.  
  25. best(Destination,List,City) :-
  26.      closest(Destination,List,City).
  27.  
  28. best(Destination,List,City) :-
  29.      closest(Destination,List,RejectedCity),
  30.      remove(RejectedCity,List,NewList),
  31.      best(Destination,NewList,City).
  32.  
  33. closest(_,[City],City).
  34.  
  35. closest(Destination,[FirstCity|RestOfCities],City) :-
  36.      closest(Destination,RestOfCities,AlternateCity),
  37.      distance(FirstCity,Destination,N),
  38.      distance(AlternateCity,Destination,M),
  39.      closer(FirstCity,AlternateCity,N,M,City).
  40.  
  41. closer(City,_,N,M,City) :- N =< M, !.
  42. closer(_,City,_,_,City).
  43.  
  44. remove(_,[],[]).
  45. remove(X,[X|Ytail],Ytail) :- !.
  46. remove(X,[Y|Ztail],[Y|Wtail]) :- remove(X,Ztail,Wtail).
  47.  
  48. connection(FirstCity,SecondCity) :-
  49.      flights_between(FirstCity,SecondCity).
  50. connection(FirstCity,SecondCity) :-
  51.      flights_between(SecondCity,FirstCity).
  52.  
  53. distance(City,City,0).
  54. distance(FirstCity,SecondCity,N) :-
  55.      miles_between(FirstCity,SecondCity,N).
  56. distance(FirstCity,SecondCity,N) :-
  57.      miles_between(SecondCity,FirstCity,N).
  58.  
  59. member(X,[X|_]).
  60. member(X,[_|Y]) :- member(X,Y).
  61.  
  62. /* Information about some cities where Slow and Low flies */
  63.  
  64. flights_between(atlanta,boston).
  65. flights_between(atlanta,cincinnati).
  66. flights_between(atlanta,new_orleans).
  67. flights_between(atlanta,washington).
  68. flights_between(cincinnati,los_angeles).
  69. flights_between(cincinnati,new_orleans).
  70. flights_between(cincinnati,washington).
  71.  
  72. miles_between(atlanta,boston,1108).
  73. miles_between(atlanta,cincinnati,400).
  74. miles_between(atlanta,los_angeles,2091).
  75. miles_between(atlanta,new_orleans,480).
  76. miles_between(atlanta,washington,618).
  77. miles_between(boston,cincinnati,840).
  78. miles_between(boston,los_angeles,3017).
  79. miles_between(boston,new_orleans,1507).
  80. miles_between(boston,washington,448).
  81. miles_between(cincinnati,los_angeles,2179).
  82. miles_between(cincinnati,new_orleans,786).
  83. miles_between(cincinnati,washington,481).
  84. miles_between(los_angeles,new_orleans,1858).
  85. miles_between(los_angeles,washington,2646).
  86. miles_between(new_orleans,washington,1099).
  87.